package com.bom_samaritano.model;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import com.bom_samaritano.Exception.BusinessException;
import com.bom_samaritano.temp.LatLng;
import com.bom_samaritano.util.Constantes;

public class Route extends VOGeneric implements Serializable {

	private static final long serialVersionUID = 1L;
	public static final String COL_ID = "id";
	public static final String COL_NAME = "lat_origin";
	public static final String COL_OWNER = "person_id";
	public static final String COL_VACANCY = "vacancy";
	public static final String TABLE = "route";
	public static final String COL_LONGITUDES = "latitudes";
	public static final String COL_LATITUDES = "longitudes";

	private int id;
	private String name;
	private String ownerEmail;
	private int vagas;
	private final List<LatLng> points;
	private String polyline;

	public Route(int id, String name, String owner, int vagas) {
		super(TABLE, COL_ID);
		this.id = id;
		this.name = name;
		this.ownerEmail = owner;
		this.vagas = vagas;
		points = new ArrayList<LatLng>();
	}

	public Route() {
		this(-1, "not_found", "not_found", 0);
	}

	public void addPoints(final List<LatLng> points) {
		this.points.addAll(points);

		String lats = Constantes.CAMPO_SEPARADOR;
		String lons = Constantes.CAMPO_SEPARADOR;
		for (LatLng latLng : points) {
			lats += latLng.latitude + Constantes.CAMPO_SEPARADOR;
			lons += latLng.longitude + Constantes.CAMPO_SEPARADOR;
		}

		if (isParamValid(COL_LATITUDES) && isParamValid(COL_LONGITUDES)) {

			lats += String.valueOf(this.getParam(COL_LATITUDES));
			lons += String.valueOf(this.getParam(COL_LONGITUDES));

			this.removeParam(COL_LATITUDES);
			this.removeParam(COL_LONGITUDES);

		}

		this.putParam(COL_LATITUDES, lats);
		this.putParam(COL_LONGITUDES, lons);
	}

	public void addPoint(final LatLng point) {
		this.points.add(point);

		String lats = "";
		String lons = "";

		lats += point.latitude + Constantes.CAMPO_SEPARADOR;
		lons += point.longitude + Constantes.CAMPO_SEPARADOR;

		if (isParamValid(COL_LATITUDES) && isParamValid(COL_LONGITUDES)) {

			lats += String.valueOf(this.getParam(COL_LATITUDES));
			lons += String.valueOf(this.getParam(COL_LONGITUDES));

			this.removeParam(COL_LATITUDES);
			this.removeParam(COL_LONGITUDES);

		}

		this.putParam(COL_LATITUDES, lats);
		this.putParam(COL_LONGITUDES, lons);

	}

	public List<LatLng> stringToPoints(String lat, String lng)
			throws BusinessException {

		String[] lats = lat.split(Constantes.CAMPO_SEPARADOR);
		String[] lons = lng.split(Constantes.CAMPO_SEPARADOR);

		if (lats.length != lons.length)
			throw new BusinessException(
					"Coordenadas invalidas! Quantidade de latitudes["
							+ lats.length + "] e longitudes[" + lons.length
							+ "] divergem");

		List<LatLng> l = new ArrayList<LatLng>();
		for (int i = 0; i < lons.length; i++) {
			if (lats[i].equals("") || lons[i].equals(""))
				continue;
			l.add(new LatLng(Double.valueOf(lats[i]), Double.valueOf(lons[i])));

		}
		return l;
	}

	public List<LatLng> getPoints() {
		return Collections.unmodifiableList(points);
	}

	public void setPolyline(String polyline) {
		this.polyline = polyline;
	}

	public String getPolyline() {
		return polyline;
	}

	public String getOwnerEmail() {
		return ownerEmail;
	}

	public void setOwnerEmail(String ownerEmail) {
		this.ownerEmail = ownerEmail;
		this.putParam(COL_OWNER, this.ownerEmail);
	}

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
		this.putParam(COL_ID, id);
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
		this.putParam(COL_NAME, this.name);
	}

	public int getVagas() {
		return vagas;
	}

	public void setVagas(int vagas) {
		this.vagas = vagas;
		this.putParam(COL_VACANCY, this.vagas);
	}

	public static Route fromVoGeneric(VOGeneric vo) {
		Route r = new Route();
		if (vo.isParamValid(COL_ID))
			r.setId(Integer.valueOf(vo.getParam(COL_ID).toString()));

		if (vo.isParamValid(COL_LATITUDES) && vo.isParamValid(COL_LONGITUDES))
			try {
				r.addPoints(r.stringToPoints(vo.getParam(COL_LATITUDES)
						.toString(), vo.getParam(COL_LONGITUDES).toString()));
			} catch (BusinessException e) {
				// TODO Auto-generated catch block
			}

		if (vo.isParamValid(COL_NAME))
			r.setName(vo.getParam(COL_NAME).toString());

		if (vo.isParamValid(COL_OWNER))
			r.setOwnerEmail(vo.getParam(COL_OWNER).toString());

		if (vo.isParamValid(COL_VACANCY))
			r.setVagas(Integer.valueOf(vo.getParam(COL_VACANCY).toString()));

		return r;
	}

	@Override
	public String toString() {
		return "Route [id=" + id + ", name=" + name + ", ownerEmail="
				+ ownerEmail + ", vagas=" + vagas + ", points=" + points
				+ ", polyline=" + polyline + "]";
	}

}
